package com.lanou3g.ptp.taskstudent.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Task;
import com.lanou3g.ptp.entity.TaskStudent;
import com.lanou3g.ptp.taskstudent.mapper.TaskStudentMapper;
import com.lanou3g.ptp.taskstudent.service.TaskStudentService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TaskStudentServiceImpl implements TaskStudentService {

    @Autowired
    TaskStudentMapper taskStudentMapper;

    @Override
    public List<TaskStudent> findAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return taskStudentMapper.findAll();
    }

    @Override
    public TaskStudent findById(Long id) {
        return taskStudentMapper.findById(id);
    }

    @Override
    public void insert(ArrayList<TaskStudent> taskStudents) {
        taskStudentMapper.insertAll(taskStudents);
    }

    @Override
    public void update(Long id, TaskStudent taskStudent) {
        taskStudent.setUpdateTime(new Date());
        taskStudentMapper.update(id, taskStudent);
    }

    @Override
    public void delete(Long id) {
        taskStudentMapper.delete(id);
    }

    @Override
    public void deletes(Long[] ids) {
        taskStudentMapper.deletes(ids);
    }

    @Override
    public void deleteByTaskId(Long taskId) {
        taskStudentMapper.deleteByTaskId(taskId);
    }

    @Override
    public void deletesByTaskIds(Long[] ids) {
        taskStudentMapper.deletesByTaskIds(ids);
    }

    @Override
    public ArrayList<Long> findByTaskId(Long id) {
        ArrayList<Long> byTaskId = taskStudentMapper.findByTaskId(id);
        return byTaskId;
    }

    @Override
    public List<TaskStudent> findAllByStudentId(Long studentId) {
        return taskStudentMapper.findAllByStudentId(studentId);
    }

    @Override
    public boolean ifStudentInTask(Long studentId, Long taskId) {
        List<Long> studentIds = taskStudentMapper.findByTaskId(taskId);
        return studentIds.contains(studentId);
    }

    @Override
    public List<Student> findStudentsByTaskId(Long taskId) {
        return taskStudentMapper.findStudentsByTaskId(taskId);
    }

    @Override
    public Boolean taskApply(Student student, long id) {
        Date date = new Date();

        List<Task> task=taskStudentMapper.selectTack(id,date);
        if (task.size()!=0){
           int add= taskStudentMapper.selectStudentTask(id,student.getId(),date);
            if (add!=0){
                return false;
            }

           int number= taskStudentMapper.addStudentTask(id,student.getId(),date);
           if (number!=0){
               return true;
           }
           return false;

        }else{
            return false;
        }



    }
}
